package com.cdp.web.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cdp.common.core.domain.AjaxResult;
import com.cdp.common.core.domain.entity.SysEfficiencyAgent;
import com.cdp.common.core.domain.entity.SysSourceName;
import com.cdp.common.core.domain.entity.SysSwellingagent;
import com.cdp.common.core.domain.entity.SysWaterReducer;
import com.cdp.system.dto.SandTypeDTO;
import com.cdp.system.service.ISysEfficiencyAgentService;
import com.cdp.system.service.ISysSourceNameService;
import com.cdp.system.service.ISysSwellingagentService;
import com.cdp.system.service.ISysWaterReducerService;
import com.cdp.system.vo.SandFineneVO;
import com.cdp.system.vo.SourceNameVO;
import org.apache.commons.math3.geometry.partitioning.AbstractRegion;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/system/sourceName")
public class SysSourceNameController {


    @Autowired
    private ISysSourceNameService sysSourceNameService;


    @Autowired
    private ISysWaterReducerService sysWaterReducerService;


    @Autowired
    private ISysEfficiencyAgentService sysEfficiencyAgentService;

    @Autowired
    private ISysSwellingagentService sysSwellingagentService;
    /**
     * 查询减水剂来源名称
     * @return
     */

    @PostMapping("listSourceName")
    private AjaxResult listSourceName(@RequestBody SandTypeDTO sandTypeDTO){

        String name = sandTypeDTO.getName();
        LambdaQueryWrapper<SysWaterReducer> wrapper = new LambdaQueryWrapper<>();
        if(!name.isEmpty()){

            wrapper.like(SysWaterReducer::getSourceName,name);
        }


        // 获取所有 SysWaterReducer 数据
        List<SysWaterReducer> list = sysWaterReducerService.list(wrapper);
        // 使用 HashSet 去重并存储 SourceNameVO 对象
        HashSet<SourceNameVO> sourceNameSet = new HashSet<>();
        for (SysWaterReducer sysWaterReducer : list) {
            String sourceName = sysWaterReducer.getSourceName();
            if (sourceName != null) {
                // 创建并设置 SourceNameVO
                SourceNameVO vo = new SourceNameVO();
                vo.setSourceName(sourceName);
                sourceNameSet.add(vo);
            }
        }
        // 转换为列表返回
        List<SourceNameVO> sourceNameList = new ArrayList<>(sourceNameSet);
        // 转换成 VO 对象，并映射字段
        List<SandFineneVO> sandFinenessVOList = sourceNameList.stream()
                .map(s -> {
                    SandFineneVO vo = new SandFineneVO();
                    vo.setValue(s.getSourceName());
                    vo.setLabel(s.getSourceName());
                    return vo;
                })
                .collect(Collectors.toList());
        return AjaxResult.success(sandFinenessVOList);
    }








    /**
     * 增效剂来源名称
     */

    @PostMapping("listSourceEfficiencyAgent")
    private AjaxResult listSourceEfficiencyAgent(@RequestBody SandTypeDTO sandTypeDTO){


        String name = sandTypeDTO.getName();
        LambdaQueryWrapper<SysEfficiencyAgent> wrapper = new LambdaQueryWrapper<>();
        if(!name.isEmpty()){

            wrapper.like(SysEfficiencyAgent::getSourceName,name);
        }
        // 获取所有 SysWaterReducer 数据
        List<SysEfficiencyAgent> list = sysEfficiencyAgentService.list(wrapper);

        // 使用 HashSet 去重并存储 SourceNameVO 对象
        HashSet<SourceNameVO> sourceNameSet = new HashSet<>();
        for (SysEfficiencyAgent sysWaterReducer : list) {
            String sourceName = sysWaterReducer.getSourceName();
            if (sourceName != null) {
                // 创建并设置 SourceNameVO
                SourceNameVO vo = new SourceNameVO();
                vo.setSourceName(sourceName);
                sourceNameSet.add(vo);
            }
        }

        // 转换为列表返回
        List<SourceNameVO> sourceNameList = new ArrayList<>(sourceNameSet);


        // 转换成 VO 对象，并映射字段
        List<SandFineneVO> sandFinenessVOList = sourceNameList.stream()
                .map(s -> {
                    SandFineneVO vo = new SandFineneVO();
                    vo.setValue(s.getSourceName());
                    vo.setLabel(s.getSourceName());
                    return vo;
                })
                .collect(Collectors.toList());


        return AjaxResult.success(sandFinenessVOList);
    }



    /**
     * 膨胀剂商家来源名称
     */

    @PostMapping("listSourceSwellingagent")
    private AjaxResult listSourceSwellingagent(@RequestBody SandTypeDTO sandTypeDTO){
        String name = sandTypeDTO.getName();
        LambdaQueryWrapper<SysSwellingagent> wrapper = new LambdaQueryWrapper<>();
        if(!name.isEmpty()){

            wrapper.like(SysSwellingagent::getSourceName,name);
        }
        // 获取所有 SysWaterReducer 数据
        List<SysSwellingagent> list = sysSwellingagentService.list(wrapper);
        // 使用 HashSet 去重并存储 SourceNameVO 对象
        HashSet<SourceNameVO> sourceNameSet = new HashSet<>();
        for (SysSwellingagent sysWaterReducer : list) {
            String sourceName = sysWaterReducer.getSourceName();
            if (sourceName != null) {
                // 创建并设置 SourceNameVO
                SourceNameVO vo = new SourceNameVO();
                vo.setSourceName(sourceName);
                sourceNameSet.add(vo);
            }
        }

        // 转换为列表返回
        List<SourceNameVO> sourceNameList = new ArrayList<>(sourceNameSet);

        // 转换成 VO 对象，并映射字段
        List<SandFineneVO> sandFinenessVOList = sourceNameList.stream()
                .map(s -> {
                    SandFineneVO vo = new SandFineneVO();
                    vo.setValue(s.getSourceName());
                    vo.setLabel(s.getSourceName());
                    return vo;
                })
                .collect(Collectors.toList());

        return AjaxResult.success(sandFinenessVOList);
    }










    /**
     * 商家来源模糊查询下拉
     */
    @PostMapping("listMistedSourceName")
    public  AjaxResult listMistedSourceName(@RequestBody SandTypeDTO sandTypeDTO){
        List<SysSourceName> sysSourceNameList = sysSourceNameService.listMistedSourceName(sandTypeDTO);
        return AjaxResult.success(sysSourceNameList);
    }






}
